package psd.model.Prolog.Converters;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.eclipse.jdt.core.dom.ASTNode;
import org.eclipse.jdt.core.dom.ForStatement;
import org.eclipse.jdt.core.dom.VariableDeclarationExpression;

import psd.model.IConverter;
import psd.model.Prolog.FactGenerationToolbox;
import psd.model.Prolog.PrologConverter;


public class PrologForStatementConverter extends PrologConverter
implements IConverter<ForStatement>{

	public PrologForStatementConverter(FactGenerationToolbox toolbox) {
		super(toolbox);
	}

	@Override
	public boolean convert(ForStatement node) {
	    List inits = node.initializers();
        List allInits = new ArrayList();
        for(Iterator i = inits.iterator(); i.hasNext();)
        {
            ASTNode next = (ASTNode)i.next();
            if(next instanceof VariableDeclarationExpression)
            {
                VariableDeclarationExpression el = (VariableDeclarationExpression)next;
                allInits.addAll(el.fragments());
            } else
            {
                allInits.add(next);
            }
        }

        String ids = idResolver.getIDs(allInits);
        String args[] = {
            ids, idResolver.getID(node.getExpression()), idResolver.getIDs(node.updaters()), idResolver.getID(node.getBody())
        };
        createBodyFact(node, "forT", args);
        return true;
	}

}
